﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiData;
using VanTaiBLL;
using VanTaiProperty;
namespace VanTaiTMS.Page
{
    public partial class chi_tiet_lenh_dieu_dong : System.Web.UI.Page
    {
        #region[Define]
        private DataTable dtSearch;
        private DataTable dtCabin;
        private DataTable dtCabinDetail;
        private DataTable dtTransferOrder;
        private DataTable dtTransferOrderDetail;
        private DataTable dtTransferOrderDrop;
        private DataTable dtDoLine;
        protected string StrAlert = string.Empty;
        private clsBusiness_TMCommandTransferOrder clsTransferOrder = new clsBusiness_TMCommandTransferOrder();
        private clsTMDoLineBusiness clsTMDoLine = new clsTMDoLineBusiness();
        private TMCabin_Business clsCabin = new TMCabin_Business();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            lbKQ_A.Visible = true;
            lbKQ_B.Visible = true;
            btnSave.Visible = false;
            if (!IsPostBack)
            {
                GetDataTransferOrder();
                GetDataDoLine();
                GetDataTranDrop();
            }
        }
        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                float totalamount = 0;
                int quality = 0;
                int totalqualytiout = 0;
                for (int i = 0; i < ListViewDoLine.Items.Count; i++)
                {
                    CheckBox chkView = (CheckBox)ListViewDoLine.Items[i].FindControl("chkListDoLine");
                    LinkButton link_Amount = (LinkButton)ListViewDoLine.Items[i].FindControl("link_Amount");
                    TextBox txtSoLuongXuat = (TextBox)ListViewDoLine.Items[i].FindControl("txtSoLuongXuat");
                    if (chkView != null)
                    {
                        if (chkView.Checked && drMaDieuDong.SelectedValue.ToString() != "-1" 
                         && drSoXe.SelectedValue.ToString() !="-1" && drChonKhoan.SelectedValue.ToString() !="-1")
                        {
                            totalamount = int.Parse(link_Amount.Text);
                            totalqualytiout = int.Parse(txtSoLuongXuat.Text);
                            quality = int.Parse(totalamount.ToString()) - int.Parse(totalqualytiout.ToString());

                            if (txtSoLuongXuat.Text == "" || txtSoLuongXuat.Text == "0")
                            {
                                lbThongBaoLoi.Text = "Số lượng xuất phải lớn hơn 0";
                                OpenDialog("#thongbao");
                                updatePanelThongBao.Update();
                            }
                            else
                            {
                                if (int.Parse(totalqualytiout.ToString()) > int.Parse(totalamount.ToString()))
                                {
                                    lbThongBaoLoi.Text = "Số lượng mặt hàng không đủ để cung cấp !";
                                    OpenDialog("#thongbao");
                                    updatePanelThongBao.Update();
                                }
                                else
                                {
                                    VanTaiProperty.clsTMDoLineProperty obj = new clsTMDoLineProperty();
                                    obj.RowID = int.Parse(ListViewDoLine.DataKeys[i].Value.ToString());
                                    obj.Amount = int.Parse(quality.ToString());
                                    VanTaiBLL.clsTMDoLineBusiness.UpdateTMDoLineQuality(obj);
                                    InsertTransferOrderLine(int.Parse(drMaDieuDong.SelectedValue.ToString()), int.Parse(txtSoLuongXuat.Text), int.Parse(ListViewDoLine.DataKeys[i].Value.ToString()));
                                    lbThongBaoLoi.Text = "Lưu mẫu tin thành công !";
                                    OpenDialog("#thongbao");
                                    updatePanelThongBao.Update();
                                }
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void cmdSerach_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert += "<div class='alert alert-warning'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Vui lòng nhập vào từ khoá để tìm kiếm !";
                StrAlert += "</div>";
            }
            else
            {
                Search(txtSearch.Text);
            }
        }
        protected void cmdRefesh_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            GetDataDoLine();
        }
        protected void cmdExport_Click(object sender, EventArgs e)
        {

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < ListViewAll.Items.Count; i++)
                {
                    CheckBox chkView = (CheckBox)ListViewAll.Items[i].FindControl("chkList");
                    for (int j = 0; j < ListViewDoLine.Items.Count; j++)
                    {
                        CheckBox chkDoLine = (CheckBox)ListViewDoLine.Items[j].FindControl("chkListDoLine");
                        if (chkView != null && chkDoLine != null)
                        {
                            if (chkView.Checked && chkDoLine.Checked)
                            {
                                InsertTransferOrderLine(int.Parse(ListViewAll.DataKeys[i].Value.ToString()), 10, int.Parse(ListViewDoLine.DataKeys[j].Value.ToString()));
                                //btnSave.Visible = false;
                                lbThongBaoLoi.Text = "Lưu mẫu tin thành công !";
                                OpenDialog("#thongbao");
                                updatePanelThongBao.Update();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void btnCheck_Click(object sender, EventArgs e)
        {
            if (txtTheTich.Text == string.Empty)
            {
                lbThongBaoLoi.Text = "Vui lòng chọn khoan để biết được thể tích !";
            }
            else
            {
                float total = 0;
                float netweight = 0;
                for (int i = 0; i < ListViewDoLine.Items.Count; i++)
                {
                    CheckBox chkDoLine = (CheckBox)ListViewDoLine.Items[i].FindControl("chkListDoLine");
                    LinkButton linkListName = (LinkButton)ListViewDoLine.Items[i].FindControl("linkListName");
                    if (chkDoLine != null)
                    {
                        if (chkDoLine.Checked)
                        {
                            total += float.Parse(linkListName.Text);
                            netweight = float.Parse(txtTheTich.Text);
                            if (total > netweight)
                            {
                                lbThongBaoLoi.Text = "Tổng thể tích của các mặt hàng là :" + total.ToString() + "<br/>";
                                lbThongBaoLoi.Text += "Tổng thể tích của 1 khoang là :" + netweight.ToString() + "<br/>";
                                lbThongBaoLoi.Text += "Quá trọng tải !";
                                OpenDialog("#thongbao");
                                updatePanelThongBao.Update();
                            }

                        }
                    }
                }
            }
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#thongbao");
        }
        //-----------------Dropdownlist---------------
        protected void drTrangThai_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void drMaDieuDong_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drMaDieuDong.SelectedValue.ToString() != "-1")
            {
                drSoXe.Items.Clear();
                drSoXe.Items.Add(new ListItem("--Chọn số xe--", "-1"));
                GetCarNo(int.Parse(drMaDieuDong.SelectedValue.ToString()));

                txtTaiXe.Enabled = false;
                txtNoiNhanHang.Enabled = false;
                txtNoiTraHang.Enabled = false;
                txtNhomXe.Enabled = false;
                txtLoaiXe.Enabled = false;
                txtNgayDieuDong.Enabled = false;
                txtTheTich.Enabled = false;
            }
            else
            {
                drSoXe.Items.Clear();
                drSoXe.Items.Add(new ListItem("--Chọn số xe--", "-1"));
                txtTaiXe.Text = string.Empty;
                txtNoiNhanHang.Text = string.Empty;
                txtNoiTraHang.Text = string.Empty;
                txtNhomXe.Text = string.Empty;
                txtLoaiXe.Text = string.Empty;
                txtNgayDieuDong.Text = string.Empty;
            }
        }
        protected void drSoXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drSoXe.SelectedValue.ToString() != "-1")
            {
                drChonKhoan.Items.Clear();
                drChonKhoan.Items.Add(new ListItem("--Chọn khoan chứa--", "-1"));
                GetCabin(int.Parse(drSoXe.SelectedValue.ToString()));
            }
            else
            {
                drChonKhoan.Items.Clear();
                drChonKhoan.Items.Add(new ListItem("--Chọn khoan chứa--", "-1"));
            }
        }
        protected void drChonKhoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drChonKhoan.SelectedValue.ToString() != "-1")
            {
                GetCabinDetail(int.Parse(drChonKhoan.SelectedValue.ToString()));
            }
            else
            {
                txtTheTich.Text = string.Empty;
            }
        }
        //-----------------List-View------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {

        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {

        }
        protected void ListViewDoLine_ItemCommand(object sender, ListViewCommandEventArgs e)
        {

        }
        protected void ListViewDoLine_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
               
            }
        }
        //-----------------DataPager------------------
        protected void DataPagerListAll_PreRender(object sender, EventArgs e)
        {
            DataPagerListAll.PageSize = 5;
            GetDataTransferOrder();
        }
        protected void DataPagerListViewDoLine_PreRender(object sender, EventArgs e)
        {
            DataPagerListViewDoLine.PageSize = 5;
            GetDataDoLine();
        }
        #endregion

        #region[Function]
        protected void GetDataTransferOrder()
        {
            dtTransferOrder = clsTransferOrder.Search(string.Empty);
            if (dtTransferOrder != null && dtTransferOrder.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtTransferOrder;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
            }
        }
        protected void GetDataTranDrop()
        {
            dtTransferOrderDrop = clsTransferOrder.Search(string.Empty);
            if (dtTransferOrderDrop != null && dtTransferOrderDrop.Rows.Count > 0)
            {
                drMaDieuDong.DataSource = dtTransferOrderDrop;
                drMaDieuDong.DataTextField = "DommandTransferOrder_Alias";
                drMaDieuDong.DataValueField = "DommandTransferOrder_ID";
                drMaDieuDong.DataBind();
            }
        }
        protected void GetDataDoLine()
        {
            dtDoLine = clsTMDoLine.Search(txtSearch.Text);
            if (dtDoLine != null && dtDoLine.Rows.Count > 0)
            {
                ListViewDoLine.DataSource = dtDoLine;
                ListViewDoLine.DataBind();
                ListViewDoLine.Visible = true;
                DataPagerListViewDoLine.Visible = true;
            }
            else
            {
                ListViewDoLine.Visible = false;
                DataPagerListViewDoLine.Visible = false;
            }
        }
        protected void GetCabin(int id)
        {
            dtCabin = clsCabin.BindCabinCarNo(id);
            if (dtCabin != null && dtCabin.Rows.Count > 0)
            {
                drChonKhoan.DataSource = dtCabin;
                drChonKhoan.DataTextField = "Cabin_Name";
                drChonKhoan.DataValueField = "Cabin_Id";
                drChonKhoan.DataBind();
            }
        }
        protected void GetCabinDetail(int id)
        {
            dtCabinDetail = clsCabin.BindCabinDetail(id);
            if (dtCabinDetail != null && dtCabinDetail.Rows.Count > 0)
            {
                txtTheTich.Text = dtCabinDetail.Rows[0]["Cabin_Total"].ToString();
            }
        }
        protected void GetCarNo(int id)
        {
            dtTransferOrderDetail = clsTransferOrder.BindGridTM_TMCommandTransferOrder_Detail_Edit(id);
            if (dtTransferOrderDetail != null && dtTransferOrderDetail.Rows.Count > 0)
            {
                drSoXe.DataSource = dtTransferOrderDetail;
                drSoXe.DataTextField = "Vehicle_Number";
                drSoXe.DataValueField = "Vehicle_ID";
                drSoXe.DataBind();

                txtNhomXe.Text = dtTransferOrderDetail.Rows[0]["VehicleGroup_Name_Old"].ToString();
                txtLoaiXe.Text = dtTransferOrderDetail.Rows[0]["Type_Vehicle_Name"].ToString();
                txtTaiXe.Text = dtTransferOrderDetail.Rows[0]["FullNameMem"].ToString();
                txtNoiNhanHang.Text = dtTransferOrderDetail.Rows[0]["Delivery_Name_REQUEST_STOCK"].ToString();
                txtNoiTraHang.Text = dtTransferOrderDetail.Rows[0]["Delivery_Name_RESPONE_STOCK"].ToString();
                txtNgayDieuDong.Text = String.Format("{0:dd/MM/yyyy}", dtTransferOrderDetail.Rows[0]["DommandTransferOrder_Date"].ToString());
            }
        }
        protected void InsertTransferOrderLine(int Order_Id, int Quality_out, int RowID)
        {
            try
            {
                TMCommandTransferOrderLine_Property obj = new TMCommandTransferOrderLine_Property();
                obj.TMCommandTransferOrder_Id = Order_Id;
                obj.Quality_out = Quality_out;
                obj.RowID = RowID;
                TMCommandTransferOrderLine_BusinessLogic.InsertTMCommandTransferOrderLine(obj);
                lbThongBaoLoi.Text = "Thêm mẫu tin thành công !";
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
            catch(Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                OpenDialog("#thongbao");
                updatePanelThongBao.Update();
            }
        }
        protected void Search(string keywords)
        {
            dtSearch = clsTMDoLine.Search(keywords);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                ListViewDoLine.DataSource = dtSearch;
                ListViewDoLine.DataBind();
                ListViewDoLine.Visible = true;
                DataPagerListViewDoLine.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtSearch.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                ListViewDoLine.Visible = false;
                DataPagerListViewDoLine.Visible = false;
                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion
    }
}